package com.longge;

import java.util.*;

public class CountPoints {

    public int countPoints(String rings) {

        char[] chars = rings.toCharArray();
        Map<Character, Set<Character>> map = new HashMap<>();
        int ans = 0;
        for (int i = chars.length - 1; i >= 0; i--) {
            char aChar = chars[i];
            if (Character.isDigit(aChar)) {
                Set<Character> characters = map.get(aChar);
                if (Objects.isNull(characters)) {
                    HashSet<Character> set = new HashSet<>();
                    map.put(aChar, set);
                }
            } else {
                Set<Character> set = map.get(chars[i + 1]);
                set.add(aChar);
            }
        }
        for (Set<Character> value : map.values()) {
            if (value.size()==3){
                ans++;
            }
        }

        return ans;
    }

    public static void main(String[] args) {
        new CountPoints().countPoints("B0B6G0R6R0R6G9");
    }

}
